/******************************************************************************
/
/  Authors:
/           Ilan Bar-Magen    061220380  silanm@t2 
/    		Shalev Mintz      066078189  shalevmi@t2  
/
/  Submition date: 6/8/2008
/
/  This file contains: europope.h implementaion, as required in assignment specs
/	
/
******************************************************************************/

#include "europope.h"

using namespace std;

StatusType EuroPope::getTown(int id, int* townId)
{	
	if (townId==NULL || id<0 || id>=citizens_num) return FAILURE; 
	int result = CitizensUF.Find(id);
	if(result == -1) return FAILURE;
	else {  *townId = result;
			return SUCCESS;
		}
}

StatusType EuroPope::getPopeTax(int town, int* tax)
{	
	if(tax==NULL || TownsDRSL.Index_By_Key(town) == -1) return FAILURE;
	*tax = (*CitizensUF.RetrieveData(town)) * (CitizensUF.GetSize(town));
	int popetax = TownsDRSL.Accumulated_By_Key(town);
	if(popetax < *tax) *tax = popetax;
	return SUCCESS;
}

StatusType EuroPope::changePopeTax(int townFrom, int townTo, int additionalTax)
{
	if(townTo<townFrom || townFrom<0 || townTo<0 || townFrom >= citizens_num || townTo>=citizens_num) return FAILURE;
	TownsDRSL.Add_Accumulation_fromKey(townFrom,additionalTax);
	TownsDRSL.Add_Accumulation_fromKey(TownsDRSL.Next_Key(townTo),-additionalTax);
	return SUCCESS;
}

StatusType EuroPope::changeSalary(int town, int newSalary)
{
	if((newSalary <= 0) || (!CitizensUF.isSet(town))) return FAILURE;
	int* town_salary = CitizensUF.RetrieveData(town);
	*town_salary = newSalary;
	return SUCCESS;
}

bool EuroPope::getTownWealth(const int town, int& wealth)
{
	if(TownsDRSL.Index_By_Key(town) == -1) return false;
	int tax = TownsDRSL.Accumulated_By_Key(town);
	int size = CitizensUF.GetSize(town);
	int salary = (*CitizensUF.RetrieveData(town));
	wealth =  salary * size  - tax;
	if (wealth < 0) wealth = 0;
	return true;
}

StatusType EuroPope::attack(int town1, int town2)
{
	int wealth1,wealth2;
	if( !getTownWealth(town1,wealth1) || !getTownWealth(town2,wealth2)) return FAILURE;
	if(wealth1 <= wealth2) return ATTACK_FAILED;
	CitizensUF.Union(town1, town2);
	int tax2 = TownsDRSL.Accumulated_By_Key(town2);
	TownsDRSL.Remove_Node(town2);
	changePopeTax(town1, town1, tax2);
	return SUCCESS;
}




